import Vue from "vue";
import VueRouter from "vue-router";
import login from "@/views/login/index.vue";
import loyout from "@/views/loyout/index.vue";
import article from "@/views/article/index.vue";
import users from "@/views/users/index.vue";
import store from "@/store";
import rights from "@/views/rights/index.vue";
import roles from "@/views/roles/index.vue";
import goods from "@/views/goods/index.vue";
import params from "@/views/params/index.vue";
import categories from "@/views/categories/index.vue";
import orders from "@/views/orders/index.vue";
import reports from "@/views/reports/index.vue";
import app from "@/views/goods/app";
Vue.use(VueRouter);
const router = new VueRouter({
  routes: [
    {
      path: "/login",
      component: login,
    },
    {
      path: "/",
      component: loyout,
      redirect: "/article",
      children: [
        {
          path: "article",
          component: article,
        },
        {
          path: "users",
          component: users,
        },
        {
          path: "rights",
          component: rights,
        },
        {
          path: "roles",
          component: roles,
        },
        {
          path: "goods",
          component: goods,
        },
        {
          path: "params",
          component: params,
        },
        {
          path: "categories",
          component: categories,
        },
        {
          path: "orders",
          component: orders,
        },
        {
          path: "reports",
          component: reports,
        },
        {
          path: "goods/app",
          component: app,
        },
      ],
    },
  ],
});
router.beforeEach((to, from, next) => {
  const token = store.state.user.token;
  if (to.path !== "/login" && !token) return next("/login");
  next();
});

export default router;
